﻿Imports System.Data.SqlClient
Public Class FrmThongKeCaNhan
    Private Sub NapLoaiTaiLieu()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select tendangnhap, hotenNguoidangnhap from login", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbNguoiDangNhap.DisplayMember = "Hotennguoidangnhap"
        CmbNguoiDangNhap.ValueMember = "tendangnhap"
        CmbNguoiDangNhap.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDataGridViewSoTheoDoiBienDong()
        Dim TuNgay As String
        TuNgay = String.Format("{0:MM/dd/yyyy}", DateTimeTuNgay.Value)
        Dim DenNgay As String
        DenNgay = String.Format("{0:MM/dd/yyyy}", DateTimeDenNgay.Value)
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select ChuSD1,  ngaydoi= convert (char (10) , bd.ngayBD, 103), GioBD, soluongBD, xa.tenxa, l.tenloaibd" &
                               " From  BIENDONG bd, login lo, tenxa xa, loaibd l WHERE " &
                               "  datediff (day, ngaynhap, '" & TuNgay & "') <= 0  " &
                               " and datediff (day, ngayNhap, '" & DenNgay & "') >= 0  " &
                               " and bd.nguoinhap = lo.tendangnhap " &
                               " and bd.diachixaid = xa.maxa " &
                               " and bd.loaibdid = l.mabd " &
                               " and bd.nguoinhap = '" & CmbNguoiDangNhap.SelectedValue.ToString & "'Order by ngaybd", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewBD.DataSource = tbl

        If (tbl.Rows.Count = 0) Then
            MessageBox.Show("không tìm thấy thông tin !")
        Else

        End If

        ob_cnn.Close()
    End Sub

    Private Sub XuatExcel()
        Dim gr As SaveFileDialog = New SaveFileDialog()
        Dim FileName As String
        With gr
            .InitialDirectory = "D:\"
            .Filter = "Microsoft Excel Worksheet (*.xls)|*.xls"
            .FilterIndex = 1
        End With
        If gr.ShowDialog = System.Windows.Forms.DialogResult.OK Then
            FileName = gr.FileName
        Else
            Exit Sub
        End If
        'Extracting from database
        Dim col, row As Integer
        Dim Excel As Object = CreateObject("Excel.Application")

        If Excel Is Nothing Then
            MsgBox("m***cel re..", MsgBoxStyle.Critical)
            Return
        End If
        'Export to Excel process
        Try
            With Excel
                .SheetsInNewWorkbook = 1
                .Workbooks.Add()
                .Worksheets(1).Select()
                .Worksheets(1).name = "So Bien Dong"
                .Range("a1:f1").Columns.Borders.Value = 1
                Dim i As Integer = 1
                For col = 0 To DataGridViewBD.Columns.Count - 1
                    .cells(1, i).value = DataGridViewBD.Columns(col).HeaderText
                    .cells(1, i).EntireRow.Font.Bold = True
                    i += 1
                Next
                i = 2
                Dim k As Integer = 1
                For col = 0 To DataGridViewBD.Columns.Count - 1
                    i = 2
                    For row = 0 To DataGridViewBD.Rows.Count - 1
                        With .worksheets(1)
                            .Cells(i, k).Value = DataGridViewBD.Rows(row).Cells(col).Value
                            i += 1
                        End With
                    Next
                    k += 1
                Next
                .ActiveCell.Worksheet.SaveAs(FileName)
            End With
            System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel)
            Excel = Nothing
            MsgBox("Dữ liệu đã được xuất sang định dạng Excel thành công!", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        ' The excel is created and opened for insert value. We most close this excel using this system
        Dim pro() As Process = System.Diagnostics.Process.GetProcessesByName("EXCEL")
        For Each i As Process In pro
            i.Kill()
        Next
    End Sub

    Private Sub FrmThongKeCaNhan_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DateTimeTuNgay.Text = Date.Today()
        DateTimeDenNgay.Text = Date.Today()
        NapLoaiTaiLieu()
        DataGridViewBD.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewBD.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
    End Sub

    Private Sub Buttimkiem_Click(sender As Object, e As EventArgs) Handles Buttimkiem.Click
        NapDataGridViewSoTheoDoiBienDong()
        ' NapDataGridViewtong()
    End Sub

    Private Sub ButtonXuatExcel_Click(sender As Object, e As EventArgs) Handles ButtonXuatExcel.Click
        XuatExcel()
    End Sub

    Private Sub ButtonThoat_Click(sender As Object, e As EventArgs) Handles ButtonThoat.Click
        Me.Close()
    End Sub

    Private Sub DateTimeTuNgay_ValueChanged(sender As Object, e As EventArgs) Handles DateTimeTuNgay.ValueChanged

    End Sub

    Private Sub DataGridViewBD_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewBD.CellContentClick

    End Sub

    Private Sub DataGridViewBD_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridViewBD.CellFormatting
        '   DataGridViewBD.Rows(e.RowIndex).HeaderCell.Value = CStr(e.RowIndex + 1)
    End Sub

    Private Sub DataGridViewBD_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewBD.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewBD.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewBD.RowHeadersWidth < Size.Width + 20 Then DataGridViewBD.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
End Class